package com.example.attendancesystem.Util;


import lombok.Data;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.time.LocalDateTime;
import java.util.Random;

@Data
public class ImageVerify {
    @Data
    public static class ImageCode {
        private BufferedImage image;
        private String code;
        private LocalDateTime expireTime;

        public ImageCode(final BufferedImage image, final String code, final int expireIn) {
            this.image = image;
            this.code = code;
            this.expireTime = LocalDateTime.now().plusSeconds(expireIn);
        }
    }

    private int width;                   //验证码图片宽度
    private int height;                   //验证码图片长度
    private int length;                    //验证码位数
    private int fontSize;                 //验证码字体大小
    private int letterSpace;    //验证码字符的间距
    private int lineSize;                //干扰线的数量
    private int expireIn;             //验证码的有效时间 单位秒
    private int positionX;                //X轴距
    private int positionY;               //Y轴距


    public ImageVerify(final int width, final int height, final int length, final int fontSize, final int expireIn, final int positionX, final int positionY) {
        this.width = width;
        this.height = height;
        this.length = length;
        this.fontSize = fontSize;
        this.letterSpace = fontSize / 2;
        this.expireIn = expireIn;
        this.positionX = positionX;
        this.positionY = positionY;
    }

    private Color getRandColor(int fc, int bc) {
        final Random random = new Random();
        if (fc > 255) fc = 255;
        if (bc > 255) bc = 255;
        final int r = fc + random.nextInt(bc - fc);
        final int g = fc + random.nextInt(bc - fc);
        final int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    public ImageCode create(HttpSession session) {
        final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        final Graphics graphics = image.getGraphics();
        graphics.setColor(getRandColor(200, 500));
        graphics.fillRect(0, 0, width, height);
        graphics.setFont(new Font("Times New Roman", Font.ITALIC, fontSize));
        graphics.setColor(getRandColor(100, 200));

        final Random random = new Random();

        //画干扰线
        for (int i = 0; i < lineSize; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int x1 = random.nextInt(12);
            int y1 = random.nextInt(12);
            graphics.drawLine(x, y, x + x1, y + y1);
        }

        //画验证码
        final StringBuffer sRand = new StringBuffer();
        for (int i = 0; i < length; i++) {
            final String str;
            // 1、2随机生成整数  如果 1就生成字符 2则生成数字
            final String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //生成字符
            if (charOrNum.equals("char")) {
                // 判断是生成大写还是小写
                int choice = random.nextInt(2) == 0 ? 65 : 97;
                str = String.valueOf((char) (choice + random.nextInt(26)));
            } else { //生成数字
                str = String.valueOf(random.nextInt(10));
            }
            // final String rand = String.valueOf(random.nextInt(10));
            sRand.append(str);
            graphics.setColor(new Color(20 + random.nextInt(10), 20 + random.nextInt(10), 20 + random.nextInt(10)));
            graphics.drawString(str, letterSpace * i + positionX, positionY);
        }
        session.setAttribute("verify", sRand);

        graphics.dispose();

        return new ImageCode(image, sRand.toString(), expireIn);
    }


}
